package com.example.smartcity_poverty.ui.AllService.JobSearch.activtiy

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_js_show_list.*
import kotlinx.coroutines.Dispatchers
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

/**
 * 招聘详情
 */
class JsShowListActivity : AppCompatActivity() {

    private val id by lazy { intent.getStringExtra("id")?.toInt() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_js_show_list)
        title = "招聘详情"
        var BossId = 0

        coroutine(Dispatchers.Main) {
            api.getJsListAll(getToken(), id!!).apply {
                if (this.code == 200) {
                    runOnUiThread {
                        this.data?.let {
                            BossId = it.companyId
                            jsListTv1.text = it.name
                            jsListTv2.text = "岗位职责: ${it.obligation}"
                            jsListTv3.text = "公司地点: ${it.address}"
                            jsListTv4.text = "薪资待遇: ${it.salary}"
                            jsListTv5.text = "联系人: ${it.contacts}"
                            jsListTv6.text = "职位描述: ${it.need}"
                            jsListTv7.text = "职位需求: ${it.need}"

                            var money = it.salary
                            coroutine(Dispatchers.Main) {
                                // 获取公司信息
                                api.getJsListBoss(getToken(), it.companyId.toInt()).apply {
                                    if (this.code == 200) {
                                        runOnUiThread {
                                            this.data?.let { i ->
                                                jsListTvv2.text = "公司名称: ${i.companyName}"
                                                jsListTvv3.text = "公司简介: ${i.introductory}"


                                                /**
                                                 * 投递简历
                                                 */
                                                jsListOk.setOnClickListener {
                                                    coroutine(Dispatchers.Main) {
                                                        api.getJsTou(
                                                            getToken(),
                                                            JSONObject().apply {
                                                                put("companyId", i.id.toString())
                                                                put("companyName", "")
                                                                put("id", 0)
                                                                put("money", money.toString())
                                                                put("postId", "0")
                                                                put("postName", "")
                                                                put("remark", "")
                                                                put("satrTime", "2023-4-12 11:52")
                                                                put("userId", "0")
                                                                put("userName", "")
                                                            }.toString()
                                                                .toRequestBody("application/json".toMediaTypeOrNull())
                                                        ).apply {
                                                            runOnUiThread { this.msg.show() }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    } else runOnUiThread { this.msg?.show() }
                                }
                            }
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        }


    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}